#!/usr/bin/make -f

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=

PACKAGE=percona-server
PS_VERSION_EXTRA = '@@PERCONA_VERSION_EXTRA@@'
REVISION = '@@REVISION@@'
COMPILATION_COMMENT_RELEASE = "Percona Server (GPL), Release $(PS_VERSION_EXTRA), Revision $(REVISION)"
COMPILATION_COMMENT_DEBUG = "Percona Server - Debug (GPL), Release $(PS_VERSION_EXTRA), Revision $(REVISION)"
TOKUDB_BACKUP_VERSION = "@@TOKUDB_BACKUP_VERSION@@"

TMP=$(CURDIR)/debian/tmp/
TMPD=$(CURDIR)/debian/tmp-debug/
prefix=/usr

ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ARCH_OS = $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
NCPU = $(shell grep -c processor /proc/cpuinfo)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

DEB_SOURCE_PACKAGE ?= $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))
DEB_VERSION ?= $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION ?= $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION ?= $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
DEB_UPSTREAM_VERSION_MAJOR_MINOR := $(shell echo $(DEB_UPSTREAM_VERSION) | sed -r -n 's/^([0-9]+\.[0-9]+).*/\1/p')
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

TOKUDB_OPTS_DEFAULT ?= -DWITH_VALGRIND=OFF -DUSE_VALGRIND=OFF -DDEBUG_EXTNAME=OFF -DBUILD_TESTING=OFF -DUSE_GTAGS=OFF -DUSE_CTAGS=OFF -DUSE_ETAGS=OFF -DUSE_CSCOPE=OFF -DTOKUDB_BACKUP_PLUGIN_VERSION=$(TOKUDB_BACKUP_VERSION)
TOKUDB_OPTS_RELEASE ?= $(TOKUDB_OPTS_DEFAULT) -DTOKU_DEBUG_PARANOID=OFF
TOKUDB_OPTS_DEBUG ?= $(TOKUDB_OPTS_DEFAULT) -DTOKU_DEBUG_PARANOID=ON

EXPORTED_SOURCE_TARBALL = debian/percona-server-source.tar.gz

DISTRIBUTION = $(shell lsb_release -i -s)
DISTRELEASE = $(shell lsb_release -c -s)

MYSQL_SRC = $(shell pwd)

builddir = builddir
builddebug = debug

override_dh_auto_configure:
	@echo "RULES.$@"

ifeq ($(SKIP_DEBUG_BINARY),)
	( test -d $(builddebug) || mkdir $(builddebug) ) && cd $(builddebug) && \
	    cmake -DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINSTALL_DOCDIR=share/mysql/docs \
		-DINSTALL_DOCREADMEDIR=share/mysql \
		-DINSTALL_INCLUDEDIR=include/mysql \
		-DINSTALL_INFODIR=share/mysql/docs \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_MANDIR=share/man \
		-DINSTALL_MYSQLSHAREDIR=share/mysql \
		-DINSTALL_MYSQLTESTDIR=lib/mysql-test \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin/debug \
		-DINSTALL_SBINDIR=sbin \
		-DINSTALL_SCRIPTDIR=bin \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DCMAKE_BUILD_TYPE=Debug \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_DEBUG) \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-DINSTALL_LAYOUT=DEB \
		-DWITH_INNODB_MEMCACHED=ON \
		-DWITH_MECAB=system \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_PAM=ON \
		-DWITH_ROCKSDB=ON \
		-DFORCE_INSOURCE_BUILD=1 \
		-DDOWNLOAD_BOOST=1 \
		-DWITH_BOOST=libboost \
		-DWITH_SYSTEM_LIBS=ON \
                -DWITH_PROTOBUF=bundled \
                -DWITH_RAPIDJSON=bundled \
                -DWITH_ICU=bundled \
                -DWITH_ZSTD=bundled \
                -DWITH_LZ4=bundled \
                -DWITH_EDITLINE=bundled \
                -DWITH_LIBEVENT=bundled \
                -DWITH_NUMA=ON \
                -DWITH_LDAP=system \
		-DWITH_EXTRA_CHARSETS=all $(TOKUDB_OPTS_DEBUG) ..
endif

	( test -d $(builddir) || mkdir $(builddir) ) && cd $(builddir) && \
	    cmake -DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINSTALL_DOCDIR=share/mysql/docs \
		-DINSTALL_DOCREADMEDIR=share/mysql \
		-DINSTALL_INCLUDEDIR=include/mysql \
		-DINSTALL_INFODIR=share/mysql/docs \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_MANDIR=share/man \
		-DINSTALL_MYSQLSHAREDIR=share/mysql \
		-DINSTALL_MYSQLTESTDIR=lib/mysql-test \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DINSTALL_SBINDIR=sbin \
		-DINSTALL_SCRIPTDIR=bin \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCOMPILATION_COMMENT=$(COMPILATION_COMMENT_RELEASE) \
		-DSYSTEM_TYPE="debian-linux-gnu" \
		-DINSTALL_LAYOUT=DEB \
		-DWITH_INNODB_MEMCACHED=ON \
		-DFORCE_INSOURCE_BUILD=1 \
		-DWITH_MECAB=system \
		-DWITH_ARCHIVE_STORAGE_ENGINE=ON \
		-DWITH_BLACKHOLE_STORAGE_ENGINE=ON \
		-DWITH_FEDERATED_STORAGE_ENGINE=ON \
		-DWITH_PAM=ON \
		-DWITH_ROCKSDB=ON \
		-DDOWNLOAD_BOOST=1 \
		-DWITH_BOOST=libboost \
		-DWITH_SYSTEM_LIBS=ON \
                -DWITH_PROTOBUF=bundled \
                -DWITH_RAPIDJSON=bundled \
                -DWITH_ICU=bundled \
                -DWITH_ZSTD=bundled \
                -DWITH_LZ4=bundled \
                -DWITH_EDITLINE=bundled \
                -DWITH_LIBEVENT=bundled \
                -DWITH_NUMA=ON \
                -DWITH_LDAP=system \
		-DWITH_EXTRA_CHARSETS=all $(TOKUDB_OPTS_RELEASE) ..

	touch $@

override_dh_auto_build:
	@echo "RULES.$@"
	
	[ -f $(EXPORTED_SOURCE_TARBALL) ] || tar -zcf $(EXPORTED_SOURCE_TARBALL) \
	--exclude=debian . \
	--transform="s,^\./,percona-server/,"

ifeq ($(SKIP_DEBUG_BINARY),)
	cd $(builddebug) && $(MAKE) -j$(NCPU)
endif

	cd $(builddir) && $(MAKE) -j$(NCPU) VERBOSE=1

	touch $@

override_dh_auto_install:
	@echo "RULES.$@"

	# complete install first
	(cd $(builddebug) && $(MAKE) install DESTDIR=$(TMPD)/)
	(cd $(builddir) && $(MAKE) install DESTDIR=$(TMP)/)

	# add Percona Server configuration file my.cnf to mysql-common package
	#install -g root -o root -m 0644 -D debian/extra/my.cnf $(TMP)/etc/mysql/my.cnf

	# add MySQL Server debug binary and library to package
	#install -g root -o root -m 0755 debian/extra/server-binary $(TMP)/usr/sbin/mysqld-debug

	# add systemd script
	install -m 0755 debian/extra/mysql-systemd-start $(TMP)/usr/share/mysql/

	# install default file for init script timeout params
	install -d $(TMP)/etc/default
	install -m 0644 debian/extra/default-mysql $(TMP)/etc/default/mysql
        # Add helper functions for maintainer scripts
	install -m 0644 debian/extra/mysql-helpers debian/tmp/usr/share/mysql/


	# install source tarball for source package
	install -D -m 0644 $(EXPORTED_SOURCE_TARBALL) $(TMP)/usr/src/percona-server/`basename $(EXPORTED_SOURCE_TARBALL)`

override_dh_strip:
	dh_strip --dbg-package=percona-server-dbg
	dh_strip -Xlibprotobuf-lite

override_dh_installinit:
	@echo "RULES.$@"
	if [ "$(DISTRELEASE)" != "precise" -a "$(DISTRELEASE)" != "trusty" -a "$(DISTRELEASE)" != "wheezy" ]; then dh_systemd_enable --name=mysql; dh_systemd_enable --no-enable --name=mysql@; fi
	dh_installinit --name=mysql -- defaults 19 21
	dh_installinit --name=mysqlrouter -- defaults 19 21
	if [ "$(DISTRELEASE)" != "precise" -a "$(DISTRELEASE)" != "trusty" -a "$(DISTRELEASE)" != "wheezy" ]; then dh_systemd_start --restart-after-upgrade; fi
	touch $@
